package com.liu.test.controller;


import com.liu.test.common.ListResponse;
import com.liu.test.common.ObjectRestResponse;
import com.liu.test.entity.Statistics;
import com.liu.test.service.StatisticService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping
@CrossOrigin
public class StatisticController {

    @Autowired
    private StatisticService statisticService;

    @GetMapping("/get")
    public ObjectRestResponse<List<Statistics>> getAll() {
        log.info("111111111");
        List<Statistics> list = statisticService.get();
        //System.out.println(list.get(0).getId());
        ObjectRestResponse result = new ObjectRestResponse();
        result.setData(list);
        result.setMessage("返回数据");

        log.info("2222222222");
        return result;
    }

    @PostMapping("/insert")
    //在postman中调试的时候需要在body一栏添加raw→json格式的内容，才能被@RequestBody注解的实体对象接收
    public ObjectRestResponse<Integer> insert(@RequestBody Statistics statistics){
        log.info("来了");
        int flag = statisticService.insertInfo(statistics);
        ObjectRestResponse result = new ObjectRestResponse();
        result.setData(flag);
        result = flag == 1 ? result.message("成功插入一条数据").status(200) : result.message("插入失败").status(0);
        log.info("别走");
        return result;
    }

}
